//
//  NMyShenbaoDetailChildVC.m
//  nncloud
//
//  Created by macfai on 2018/11/22.
//  Copyright © 2018 macfai. All rights reserved.
//
#import "NAccessoryCell.h"
#import "NMyShenbaoDetailChildVC.h"
#import "NShenbaoTypeCell.h"
#import "NMyShenbaoModel.h"
#import "NFujianModel.h"
#import "NFuJianCell.h"
@interface NMyShenbaoDetailChildVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSArray *titleArray;
@property (nonatomic,strong)NSArray *infoArray;
@property (nonatomic,strong)NSArray *fujianArray;
@property (nonatomic,strong)NSArray *fujianInfoArray;
@property (nonatomic,strong)NSMutableArray *dataArray;

@end

@implementation NMyShenbaoDetailChildVC
#define cellIdentifier @"shenbaoType"
#define fujianCellIdentifier @"accessory"
-(UITableView *)tableView{
    
    if (!_tableView) {
        
        WeakStament(ws);
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 0, ScreenWidth , ScreenHeight - 64- 50) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
//        _tableView.rowHeight = 60.f;
        //        _tableView.separatorColor = [UIColor clearColor];
        _tableView.backgroundColor = kBackgroundColor;
        [_tableView registerNib:[UINib nibWithNibName:@"NShenbaoTypeCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
         [_tableView registerNib:[UINib nibWithNibName:@"NAccessoryCell" bundle:nil] forCellReuseIdentifier:fujianCellIdentifier];
        //        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //                        [ws loadData];
        //        }];
        //        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        //            [ws loadMoreData];
        //        }];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArray = [NSMutableArray array];
    //我的申报详情
     [self.navigationItem setItemWithTitle:@"项目详情" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    
    self.titleArray = @[@"项目名称",@"项目总投资（万元）",@"申报日期",@"申报类型",@"建设起始年限",@"建设截止年限",@"建设地点",@"申请扶助资金（万元）",@"自由资金（万元）",@"新增税金（万元）",@"新增销售收入（万元）",@"新增利润（万元）",@"企业名称",@"负责人",@"联系电话",];
    self.fujianArray = @[@"附件名称",@"附件要求",@"是否必须",@"附件备注"];
    self.fujianInfoArray = @[@"企业标识截图",@"不大于1M的图片",@"是",@"无"];
    [self.view addSubview:self.tableView];
    //查看附件暂时隐藏
//    [self addFooter];
    [self loadData];
    // Do any additional setup after loading the view from its nib.
}


///这里面包含附件的相关内容

- (void)loadData{
    
    NSDictionary *dict = @{@"declarationId":[NUserModel shareInstance].shenbaoId};
    
    [[NNRCNetRequest manager]getWithUrl:kMyShenbaoDetailUrl params:dict success:^(id result) {
        YYLog(@"%@",result);
        WeakStament(ws);
        if ([[result objectForKey:@"code"] integerValue] == 0) {
            NSDictionary *dict = result[@"page"][@"detail"];
            //项目信息数据
            NMyShenbaoModel *model = [NMyShenbaoModel mj_objectWithKeyValues:dict];
            YYLog(@"%@",model);
            [ws formDataWithModel:model];
            
            //附件数据
            NSArray *arr = result[@"page"][@"attachmentsTable"];
            YYLog(@"附件没有转之前的数组：%@",arr);
            for (NSDictionary *dic in arr) {
                NFujianModel *model = [NFujianModel mj_objectWithKeyValues:dic];
                //model中有一个数组属性
                YYLog(@"附件没有转之前的数组：%@",model.fileList);
                [ws.dataArray addObject:model];
            }

            [ws.tableView reloadData];
        }else{
            [AFToast showText:result[@"msg"]];
        }
    } failure:^(id failure) {
        
    }];
}

- (void)formDataWithModel:(NMyShenbaoModel *)model {
    
    ///这个id在流转页面下使用
    [NGlobalModel shareInstance].shenbaoId = model.declarationId;
    NSString *name = model.declarationName;
    NSString *touzi =[NSString stringWithFormat:@"%@",model.declarationInvestment];
    NSString *riqi = [NSString stringWithFormat:@"%@",model.declarationTime];//
    NSString *leixing = model.declarationType;
    NSString *startTime = [[ToolKit getTimeFromTimesTamp:model.declarationStartTime] substringToIndex:10];
    NSString *endTime =  [[ToolKit getTimeFromTimesTamp:model.declarationEndTime] substringToIndex:10];
    NSString *area = model.declarationAddress;
    NSString *shenqingjijin = model.declarationSupportFund;
    NSString *selffund = model.declarationOwnFund;
    NSString *newTax = model.declarationNewTaxes;//民营还是国企
    NSString *newIncome =  model.declarationSalesRevenue;
    NSString *newprofit = model.declarationProfit;
    NSString *qiyeName  = model.declarationEnterprise;
    NSString *fuzeren = model.declarationPerson;
    NSString *tel = model.declarationPhone;
    
    self.infoArray = @[name,touzi,riqi,leixing,startTime,endTime,area,shenqingjijin,selffund,newTax,newIncome,newprofit,qiyeName,fuzeren,tel];
}

-(void)addFooter {
    UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 100.f)];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 20, 300, 50);
    btn.centerX = footer.centerX;
    btn.backgroundColor = ThemeBlue;
    btn.layer.cornerRadius = 5.f;
    btn.layer.masksToBounds = YES;
    [btn setTitle:@"查看附件" forState:UIControlStateNormal];
    [btn addTarget: self action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];
    [footer addSubview:btn];
    self.tableView.tableFooterView = footer;
}

- (void)tapAction:(UIButton *)btn
{
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return 60.f;
    }else{
        return 70.f;
    }
}

#define kSectionHeight 70.f
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (section==1) {
        
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, kSectionHeight)];
        view.backgroundColor = kBackgroundColor;
        
        UIView *secondView = [[UIView alloc]initWithFrame:CGRectMake(0, 10, ScreenWidth, kSectionHeight)];
        secondView.backgroundColor = [UIColor whiteColor];
        [view addSubview:secondView];
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 23, 300.f, 20.f)];
        label.text = @"申报附件";
        [secondView addSubview:label];
        
        UIView *line = [[UIView alloc]initWithFrame:CGRectMake(20.f, 58.f, ScreenWidth-10, 0.3)];
        line.backgroundColor = kLightGrayColor;
        [secondView addSubview:line];
        return view;
    }
    return nil;
}



- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section==0) {
        return 0.01;
    }else{
        return kSectionHeight;
    }
}
#pragma mark - datasource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0) {
        
        return self.titleArray.count;
        
    }else{
//        return self.fujianArray.count;
        return self.dataArray.count;
//        NFujianModel *model = [self.dataArray lastObject];
//        return model.fileList.count;
        
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    

    
    if (indexPath.section==0) {
        
        NShenbaoTypeCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
//        NMyShenbaoModel *model = [self.dataArray lastObject];
        cell.name.text = self.titleArray[indexPath.row];
        cell.levelLab.text = self.infoArray[indexPath.row];
        cell.timeLab.hidden  = YES;
        cell.keshiLab.hidden = YES;
        return cell;
        
    }else if(indexPath.section==1){
        
        NFujianModel *model = self.dataArray[indexPath.row];
        NAccessoryCell *cell = [tableView dequeueReusableCellWithIdentifier:fujianCellIdentifier forIndexPath:indexPath];
        cell.nameLabel.text = model.fileName;
        //循环创建按钮
//        for (NSDictionary *dic in model.fileList) {
//            NFujianModel *smodel = [NFujianModel mj_objectWithKeyValues:dic];
//            cell.model = smodel;
//        }
//        [self addDownLoadLabelWithCell:cell];
//        for (int i = 0;i<model.fileList.count;i++) {
//            //根据数组个数动态创建
//            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(i*70+25, 8, 65, 35)];
//        }
        return cell;
    }
    return nil;
}

///添加下载按钮
- (void)addDownLoadLabelWithCell:(NFuJianCell*)cell{
    
//    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(30, 5, 90, 35)];
//    label.text = @"下载";
//    label.font = kFont14;
//    label.textColor = kWhiteColor;
//    label.textAlignment = NSTextAlignmentCenter;
//    label.layer.cornerRadius = 5.f;
//    label.layer.masksToBounds = YES;
//    label.backgroundColor = ThemeBlue;
//    [cell.fujianView addSubview:label];
//    label.userInteractionEnabled = YES;
//    //            label.tag = i+100;
//    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageClick:)];
//    [label addGestureRecognizer:tapGestureRecognizer];
}

-(void)imageClick:(UITapGestureRecognizer *)tap{
    //    NSLog(@"点击图片");
    UIImageView *clickedImageView = (UIImageView *)tap.view;
    //执行下载过程
    [SVProgressHUD show];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [SVProgressHUD dismiss];
        [AFToast showText:@"下载完成"];
    });
//    [ImageZoom imageZoomWithImageView:clickedImageView];
}

@end
